<?php

require_once dirname(__FILE__).'/../lib/objetivo_educacionalGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/objetivo_educacionalGeneratorHelper.class.php';

class objetivo_educacionalActions extends autoObjetivo_educacionalActions
{

	public function execute($request)
	{
		$carrera_slug = $request->getUrlParameter('carrera_slug');

		if (!CarreraUsuarioTable::getInstance()->existsAccess($carrera_slug, $this->getUser()->getUserId()))
			$this->forward('login', 'secure');

		$this->getContext()->getRouting()->setDefaultParameter('carrera_slug', $carrera_slug);

		if ($id = $request->getUrlParameter('id'))
			$this->getContext()->getRouting()->setDefaultParameter('id', $id);

		return parent::execute($request);
	}

	public function executeIndex(sfWebRequest $request)
	{
		parent::executeIndex($request);
		$carrera = CarreraTable::getInstance()->getNameBySlug($request->getParameter('carrera_slug'));
		$this->getUser()->setFlash('info', $carrera, false);
	}

	public function executeNew(sfWebRequest $request)
	{
		$carrera_id = CarreraTable::getInstance()->getIdBySlug($request->getUrlParameter('carrera_slug'));
		$this->objetivo_educacional = new ObjetivoEducacional();
		$this->objetivo_educacional->setCarreraId($carrera_id);
		
		$this->form = $this->configuration->getForm($this->objetivo_educacional);
	}

	protected function buildQuery()
	{
		$query = parent::buildQuery();

		$alias = $query->getRootAlias();
		$query->select($alias . '.*');
		$query->andWhere( 'c.slug = ?', $this->getRequest()->getUrlParameter('carrera_slug'));
		$query->orderBy('tipo DESC');

		CarreraUsuarioTable::addUserAccessQuery($query, $alias);

		return $query;
	}

}
